/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.mq.httpservice;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class HttpSender {

    static Logger logger = Logger.getLogger(HttpSender.class.getName());
    public String request;
    public String url;

    public HttpSender() {
    }

    public HttpSender(String request, String url) {
        this.request = request;
        this.url = url;
    }

    public String getRequest() {
        return request;
    }

    public void setRequest(String request) {
        this.request = request;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

//    public String closableHttpClient(String request, String url) {
//
//        String result = "";
//        HttpPost httppost = null;
//        CloseableHttpClient httpclient = null;
//        try {
//            // 创建默认的httpClient实例.    
//            httpclient = HttpClients.createDefault();
//            httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
//            httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 3000);
//            // 创建httppost    
//            httppost = new HttpPost(url);
//            // 创建参数队列    
////        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
////        formparams.add(new BasicNameValuePair("username", "admin"));
////        formparams.add(new BasicNameValuePair("password", "123456"));
////        UrlEncodedFormEntity uefEntity;
//            StringEntity reqEntity = new StringEntity(request, Charset.forName("UTF-8"));
//            reqEntity.setContentType("application/json");// 区分不同种类的数据,xml数据
//
////            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
//            httppost.setEntity(reqEntity);
//            System.out.println("executing request " + httppost.getURI());
//            CloseableHttpResponse response = httpclient.execute(httppost);
//            try {
////                HttpEntity entity = response.getEntity();
////                System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
//                StatusLine sl = response.getStatusLine();
//                if (sl.getStatusCode() == 200) {
//                    String rs = HttpResponseParser.getResponseContent(response);
//                    result = rs;
//                } else {
//                    result = "";
//                    logger.warn("response code is " + sl.getStatusCode() + "\nresponse content is \t" + result); //                System.out.println(response.toString());
//                }
//            } finally {
//                response.close();
//            }
//        } catch (ClientProtocolException e) {
//            logger.error(e.getMessage(), e);
//        } catch (UnsupportedEncodingException e1) {
//            logger.error(e1.getMessage(), e1);
//        } catch (IOException e) {
//            logger.error(e.getMessage(), e);
//        } finally {
//            // 关闭连接,释放资源    
//            try {
//                httppost.releaseConnection();
//                httpclient.close();
//            } catch (IOException e) {
//                logger.error(e.getMessage(), e);
//            }
//        }
//        return result;
//    }
public String closableHttpClient(String request, String url) {
        String result = "";
        // 创建默认的httpClient实例.    
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httppost    
        HttpPost httppost = new HttpPost(url);
        // 创建参数队列    
//        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
//        formparams.add(new BasicNameValuePair("username", "admin"));
//        formparams.add(new BasicNameValuePair("password", "123456"));
//        UrlEncodedFormEntity uefEntity;
        StringEntity reqEntity = new StringEntity(request, Charset.forName("UTF-8"));
        reqEntity.setContentType("application/json");// 区分不同种类的数据,xml数据
        try {
//            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(reqEntity);
            System.out.println("executing request " + httppost.getURI());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
//                HttpEntity entity = response.getEntity();
//                System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
                StatusLine sl = response.getStatusLine();
                if (sl.getStatusCode() == 200) {
                    String rs = HttpResponseParser.getResponseContent(response);
                    result = rs;
                } else {
                    result = "";
                    logger.warn("response code is " + sl.getStatusCode() + "\nresponse content is \t" + result); //                System.out.println(response.toString());
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            logger.error(e.getMessage(), e);
        } catch (UnsupportedEncodingException e1) {
            logger.error(e1.getMessage(), e1);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } finally {
            // 关闭连接,释放资源    
            try {
                httppost.releaseConnection();
                httpclient.close();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
        return result;
    }
    public static String sendStringByUrl(String request, String url) {
        String result = "";
        HttpPost httppost1 = null;
        HttpClient httpClient1 = null;
        try {
            httpClient1 = new DefaultHttpClient();
            //设置超时5s
            httpClient1.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
            httpClient1.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
            httppost1 = new HttpPost(url);
            StringEntity reqEntity = new StringEntity(request, Charset.forName("UTF-8"));
            reqEntity.setContentType("application/json");// 区分不同种类的数据,xml数据
            httppost1.setEntity(reqEntity);
            HttpResponse response = httpClient1.execute(httppost1);
            StatusLine sl = response.getStatusLine();
            if (sl.getStatusCode() == 200) {
                String rs = HttpResponseParser.getResponseContent(response);
                result = rs;
            } else {
                result = "";
                logger.warn("response code is " + sl.getStatusCode() + "\nresponse content is \t" + result); //                System.out.println(response.toString());
            }
        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
        } finally {
            httppost1.releaseConnection();
            httpClient1.getConnectionManager().shutdown();
        }
        return result;
    }

    public InputStream picInputStream(String requestkey, String requestvalue, String post) {
        HttpPost httppost1 = null;
        HttpClient httpClient1 = null;
        try {
            httpClient1 = new DefaultHttpClient();
            //设置超时60s
            httpClient1.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
            httpClient1.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
            httppost1 = new HttpPost(post);
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair(requestkey, requestvalue));

            httppost1.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
            HttpResponse response = httpClient1.execute(httppost1);
            StatusLine sl = response.getStatusLine();
            if (sl.getStatusCode() == 200) {
                return response.getEntity().getContent();
            } else {
                logger.warn("response code is " + sl.getStatusCode()); //                System.out.println(response.toString());
                return null;
            }
        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
            return null;
        } finally {
            httppost1.releaseConnection();
            httpClient1.getConnectionManager().shutdown();
        }
    }

    public static String senRequest(String request, String url) {
        String result = "";
        HttpPost httppost1 = null;
        HttpClient httpClient1 = null;
        try {
            httpClient1 = new DefaultHttpClient();
            //设置超时5s
            httpClient1.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 500);
            httpClient1.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 500);
            httppost1 = new HttpPost(url);
            StringEntity reqEntity = new StringEntity(request, Charset.forName("UTF-8"));
            reqEntity.setContentType("application/json");// 区分不同种类的数据,xml数据
            httppost1.setEntity(reqEntity);
            HttpResponse response = httpClient1.execute(httppost1);
            StatusLine sl = response.getStatusLine();
            if (sl.getStatusCode() == 200) {
                String rs = HttpResponseParser.getResponseContent(response);
                result = rs;
            } else {
                result = "";
                logger.warn("response code is " + sl.getStatusCode() + "\nresponse content is \t" + result); //                System.out.println(response.toString());
            }
        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
        } finally {
            httppost1.releaseConnection();
            httpClient1.getConnectionManager().shutdown();
        }
        return result;

    }

}
